//
//  PublicFunction.m
//  takeout
//
//  Created by Derekc on 12-3-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "PublicFunction.h"
#import "Constants.h"

@implementation PublicFunction

+ (void) CreateImageFolder{
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Images/"];
    NSFileManager *fileManage = [NSFileManager defaultManager];
    if(![fileManage fileExistsAtPath:documentsDirectory]) {
        [fileManage createDirectoryAtPath:documentsDirectory withIntermediateDirectories:NO attributes:nil error:nil];
    }
}

+ (void) RemoveAllImages{
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Images/"];
    NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
    if (fileList == nil || [fileList count] == 0) {
        return;
    }
    for(int i=0;i<[fileList count]; i++){
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingString:@"/Images/"];
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[fileList objectAtIndex:i]];
        NSURL *localFilePath=[NSURL fileURLWithPath:filePath];
        [[NSFileManager defaultManager] removeItemAtURL:localFilePath error:nil];
    }
}

+(NSString *) getLocalImagePath:(NSString *)imgUrl{
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/"];
	NSString *fullFilePath = [[NSString alloc] initWithFormat:@"%@/Images/%@", documentsDirectory, [[imgUrl componentsSeparatedByString:@"/"] objectAtIndex:[[imgUrl componentsSeparatedByString:@"/"] count] -1]];
    return fullFilePath;
}

+(NSString *) getLocalImageName:(NSString *)imgUrl{
	NSString *imageName = [[imgUrl componentsSeparatedByString:@"/"] objectAtIndex:[[imgUrl componentsSeparatedByString:@"/"] count] -1];
    return imageName;
}

+ (UIImage*) imageWithName:(NSString*) name{
    UIImage* image = nil;
    if(name){
        NSString* localFilePath = [NSHomeDirectory()stringByAppendingPathComponent:IMAGE_FOLDER];
        localFilePath = [localFilePath stringByAppendingPathComponent:name];
        NSFileManager* fileManger = [NSFileManager defaultManager];
        if([fileManger fileExistsAtPath:localFilePath]){
            image = [UIImage imageWithContentsOfFile:localFilePath];
        }
    }
    
    return image;
}

+ (void) saveImageData:(NSData*) imageData withName:(NSString*)name{
    if(imageData && name){
        NSString* localFilePath = [NSHomeDirectory()stringByAppendingPathComponent:IMAGE_FOLDER];
        localFilePath = [localFilePath stringByAppendingPathComponent:name];
        NSFileManager* fileManger = [NSFileManager defaultManager];
        if([fileManger fileExistsAtPath:localFilePath]){
            [fileManger removeItemAtPath:localFilePath error:nil];
        }
        [imageData writeToFile:localFilePath atomically:NO];
    }
}

+ (BOOL) isExistImage:(NSString*) imgUrl{
    NSString *fullFilePath =[self getLocalImagePath:imgUrl];
	NSFileManager *fileManager = [NSFileManager defaultManager];
    return [fileManager fileExistsAtPath:fullFilePath];
}

+ (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
    
    float width;
    float height;
    if (image.size.width > newSize.width || image.size.height > newSize.height){
        if (image.size.width > image.size.height) {
            height = (image.size.height / image.size.width * newSize.width); 
            width = newSize.width;
            //NSLog(@"%f",height);
        }
        else {
            width = image.size.width / image.size.height * newSize.height;
            //NSLog(@"%f",width);
            height=newSize.height;
        }
    }
    else {
        
        width = newSize.width;
        height = newSize.height;  
    }
    newSize = CGSizeMake(width, height);
    
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGImageRef imageRef = image.CGImage;
    
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);
    
    CGContextConcatCTM(context, flipVertical);  
    // Draw into the context; this scales the image
    CGContextDrawImage(context, newRect, imageRef);
    
    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    
    CGImageRelease(newImageRef);
    UIGraphicsEndImageContext();    
    
    return newImage;
}
@end
